package cn.iocoder.yudao.module.product.controller.admin.emissionsource;

import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.*;
import org.springframework.util.Assert;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import java.io.IOException;
import java.util.*;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.module.product.controller.admin.emissionsource.vo.*;
import cn.iocoder.yudao.module.product.dal.dataobject.emissionsource.EmissionSourceDO;
import cn.iocoder.yudao.module.product.convert.emissionsource.EmissionSourceConvert;
import cn.iocoder.yudao.module.product.service.emissionsource.EmissionSourceService;
import org.springframework.web.client.RestTemplate;

@Tag(name =  "管理后台 - 排放源")
@RestController
@RequestMapping("/product/emission-source")
@Validated
public class EmissionSourceController {

    private static final String BASE_URL = "http://127.0.0.1:7862";
    private final RestTemplate restTemplate = new RestTemplate();

    @Resource
    private EmissionSourceService emissionSourceService;

    @PostMapping("/create")
    @Operation(summary = "创建排放源")
    @PreAuthorize("@ss.hasPermission('product:emission-source:create')")
    public CommonResult<Long> createEmissionSource(@Valid @RequestBody EmissionSourceCreateReqVO createReqVO) {
        return success(emissionSourceService.createEmissionSource(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新排放源")
    @PreAuthorize("@ss.hasPermission('product:emission-source:update')")
    public CommonResult<Boolean> updateEmissionSource(@Valid @RequestBody EmissionSourceUpdateReqVO updateReqVO) {
        emissionSourceService.updateEmissionSource(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除排放源")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('product:emission-source:delete')")
    public CommonResult<Boolean> deleteEmissionSource(@RequestParam("id") Long id) {
        emissionSourceService.deleteEmissionSource(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得排放源")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('product:emission-source:query')")
    public CommonResult<EmissionSourceRespVO> getEmissionSource(@RequestParam("id") Long id) {
        EmissionSourceDO emissionSource = emissionSourceService.getEmissionSource(id);
        return success(EmissionSourceConvert.INSTANCE.convertRespVO(emissionSource));
    }

    @GetMapping("/list")
    @Operation(summary = "获得排放源列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('product:emission-source:query')")
    public CommonResult<List<EmissionSourceRespVO>> getEmissionSourceList(@RequestParam("ids") Collection<Long> ids) {
        List<EmissionSourceDO> list = emissionSourceService.getEmissionSourceList(ids);
        return success(EmissionSourceConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得排放源分页")
    @PreAuthorize("@ss.hasPermission('product:emission-source:query')")
    public CommonResult<PageResult<EmissionSourceRespVO>> getEmissionSourcePage(@Valid EmissionSourcePageReqVO pageVO) {
        PageResult<EmissionSourceDO> pageResult = emissionSourceService.getEmissionSourcePage(pageVO);
        return success(EmissionSourceConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/recommend")
    @Operation(summary = "推荐排放源")
    @PreAuthorize("@ss.hasPermission('product:emission-source:recommend')")
    public CommonResult<Object> recommendEmissions(@Valid Long id){
        // 1.1 构建请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        // 1.2 构建请求参数
        MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
        // 2. 执行请求
        ResponseEntity<Object> exchange = restTemplate.exchange(
                BASE_URL + "/recommendEmissions?id={id}",
                HttpMethod.GET,
                new HttpEntity<>(body, headers),
                new ParameterizedTypeReference<Object>() {
                },
                id); // 解决 CommonResult 的泛型丢失
        Assert.isTrue(exchange.getStatusCode().is2xxSuccessful(), "响应必须是 200 成功");
        System.out.println(exchange.getBody());
        return success(exchange.getBody());
    }

}
